
package com.wyzz.hr.base;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.math.BigDecimal;

import com.wyzz.hr.util.MyTools;


public class DaoBean extends BaseDomain implements Serializable, Cloneable {
	public DaoBean() {
	}

	public Object clone() throws CloneNotSupportedException {
		return super.clone();
	}

	public void setEncry(String fieldName, Object contentValue, String id,
			int flag) {
		if (contentValue == null)
			return;
		try {
			Class cls[] = { contentValue.getClass() };
			Method method = getClass().getMethod(
					(new StringBuilder()).append("set").append(
							fieldName.substring(0, 1).toUpperCase()).append(
							fieldName.substring(1, fieldName.length()))
							.toString(), cls);
			String key = setUpKey(id, flag);
			if (flag == MyTools.STRING) {
				String encrypto = MyTools.vigenere(contentValue.toString(),
						key, MyTools.ENCRYPT_MODE);
				Object objs[] = { encrypto };
				method.invoke(this, objs);
			} else if (flag == MyTools.CHINESE) {
				String encrypto = MyTools.vigenereChinese(contentValue
						.toString(), key, MyTools.ENCRYPT_MODE);
				Object objs[] = { encrypto };
				method.invoke(this, objs);
			} else if (flag == MyTools.BIGDECIMAL) {
				String encryptString = ((BigDecimal) contentValue).toString();
				if (encryptString.indexOf('.') == -1)
					encryptString = (new StringBuilder()).append(encryptString)
							.append(".00").toString();
				BigDecimal enctrypUse = new BigDecimal(encryptString);
				BigDecimal encrypto = MyTools.vigenere(enctrypUse, key,
						MyTools.ENCRYPT_MODE);
				Object objs[] = { encrypto };
				method.invoke(this, objs);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Object getDecry(String content, String id, int flag) {
		String key = setUpKey(id, flag);
		try {
			Method method = getClass().getMethod(
					(new StringBuilder()).append("get").append(
							content.substring(0, 1).toUpperCase()).append(
							content.substring(1, content.length())).toString(),
					new Class[0]);
			Object decrypto = method.invoke(this, new Object[0]);
			if (decrypto != null
					&& decrypto.getClass().equals(java.lang.String.class))
				if (flag == MyTools.STRING)
					decrypto = MyTools.vigenere((String) decrypto, key,
							MyTools.DECRYPT_MODE);
				else if (flag == MyTools.CHINESE)
					decrypto = MyTools.vigenereChinese((String) decrypto, key,
							MyTools.DECRYPT_MODE);
			if (decrypto != null
					&& decrypto.getClass().equals(java.math.BigDecimal.class))
				decrypto = MyTools.vigenere((BigDecimal) decrypto, key,
						MyTools.DECRYPT_MODE);
			return decrypto;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	protected String setUpKey(String id, int flag) {
		return MyTools.getUpKey(id, flag);
	}

	private static final long serialVersionUID = 1L;
}
